<template>
  <div class="divBox">
    <div slot="header" class="clearfix">
      <div class="container">
        <el-form inline>
          <el-form-item label="角色名称：">
            <el-input v-model="tableFrom.homeName" placeholder="请输入" class="selWidth" clearable />
          </el-form-item>

          <el-form-item label="所属公司：">
            <el-select v-model="tableFrom.status" placeholder="请选择" clearable style="width: 320px">
              <el-option
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-button type="primary" icon="el-icon-search" @click="getList(1)">搜索</el-button>
        </el-form>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="tableData.data"
      @row-click="oneLine"
      size="mini"
      ref="multipleTable"
      :header-cell-style=" {fontWeight:'bold'}"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号"  type="index" /> -->
      <!-- <el-table-column prop="sort" label="排序"/> -->

      <el-table-column prop="homeAddress" label="角色ID" />
      <el-table-column prop="homeAddress" label="角色名称" />
      <el-table-column prop="homeAddress" label="所属公司" />
      <el-table-column prop="homeAddress" label="创建人" />

      <el-table-column label="状态" min-width="200" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
        </template>
      </el-table-column>
    </el-table>

    <div class="block mb20">
      <el-pagination
        :page-sizes="[10, 20, 30, 40]"
        :page-size="tableFrom.limit"
        :current-page="tableFrom.page"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
        @size-change="handleSizeChange"
        @current-change="pageChange"
      />
    </div>
    <div class="bt" style="text-align: center;">
      <el-button
        style="width:100px"
        type="primary"
        @click="submitForm('formValidate')"
        v-if="isCreate!=2"
      >提交</el-button>
      <el-button style="width:100px" @click="close">取消</el-button>
    </div>
  </div>
</template>
      
      <script>
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
  refreshCache
} from "@/api/system/dict/type";

import { homeGetList, homeGetDelete } from "@/api/village";
export default {
  name: "advertisingSpaceManage",

  dicts: ["sys_normal_disable"],
  data() {
    return {
      status: 1,
      tableDatas: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄"
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄"
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄"
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄"
        }
      ],
      multiple: true,
      showSearch: true,
      single: true,
      time: undefined,
      changePassWordPup: false,
      dialogVisible: false,
      isCreate: 0,
      selectItem: undefined,
      listLoading: true,
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        name: undefined, //名称
        isShow: undefined //抢购状态
      },
      loading: false,
      categoryOptions: [],
      ids: []
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 保存提交
    submitForm() {
      console.log(this.lll, "lllllllll");
      console.log(this.tableData, "这是什么呀?");
      if (this.isCreate === 0) {
        //新增
        this.$refs.formValidate.validate(valid => {
          this.formValidate.medalId = this.formValidate.medalId.toString();
          console.log(this.formValidate, "this.formValidatethis.formValidate");
          if (valid) {
            homeGetAdd(this.formValidate).then(res => {
              this.$message.success("提交成功");
              this.$emit("getList");
              this.$emit("close");
            });
          }
        });
      }

      if (this.isCreate === 1) {
        //编辑
        this.formValidate.medalId = this.formValidate.medalId.toString();
        console.log(this.formValidate, "2this.formValidatethis.formValidate");
        this.$refs.formValidate.validate(valid => {
          if (valid) {
            homeGetEdit(this.formValidate).then(res => {
              this.$message.success("提交成功");
              this.$emit("getList");
              this.$emit("close");
            });
          }
        });
      }
    },
    close() {
      this.$emit("close");
    },
    changePassWord() {
      this.changePassWordPup = true;
    },
    resetPassWord() {
      this.$modal
        .confirm("确定要重置密码吗？初始密码为：123456")
        .then(() => {
          this.loading = true;
          return delModel(modelIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
      // let data={
      //   id:id
      // }
      this.$modalSure().then(() => {
        homeGetDelete({ id: id }).then(() => {
          this.$message.success("删除成功");
          this.tableData.data.splice(idx, 1);
        });
      });
    },
    oneLine(e) {
      console.log(e);
    },
    handleSelectionChange(selection) {
      // this.ids = [];

      // if (list.length > 1) {
      //   this.$refs.table.clearSelection();
      //   this.$refs.table.toggleRowSelection(
      //     list[list.length - 1]
      //   );
      // }
      // this.ids = [list[list.length - 1]];
      // this.ids =  this.ids.map(item => item.roleId)
      // // console.log(this.ids,'this.ids')
      // return;
      // ?console.log(selection,'selectionselection')
      this.ids = selection.map(item => item.roleId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      console.log(this.ids, "idsids");
    },
    // 删除
    handleDelete(id, idx) {
      this.$modal
        .confirm("确定要删除吗？")
        .then(() => {
          this.loading = true;
          return delModel(modelIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
      // let data={
      //   id:id
      // }
      this.$modalSure().then(() => {
        homeGetDelete({ id: id }).then(() => {
          this.$message.success("删除成功");
          this.tableData.data.splice(idx, 1);
        });
      });
    },

    // 编辑
    handleEdit(type, row) {
      if (type == 1) {
        this.isCreate = 1;
      } else if (type == 2) {
        this.isCreate = 2;
      }
      this.selectItem = row;

      this.dialogVisible = true;
    },

    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      listType(this.tableFrom)
        .then(res => {
          this.tableData.data = res.rows;
          this.tableData.total = res.total;
          this.tableData.data.map(item => this.$set(item, "isEdit", false));
          this.listLoading = false;
        })
        .catch(res => {
          this.listLoading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    handleStatusChange(id, name) {
      if (name === "上架") {
        postCateringStatus({ ids: id, isShow: 1 }).then(() => {
          this.getList(1);
        });
      }

      if (name === "下架") {
        postCateringStatus({ ids: id, isShow: 0 }).then(() => {
          this.getList(1);
        });
      }
    },
    add() {
      this.selectItem = undefined;
      this.isCreate = 0;
      this.dialogVisible = true;
    },
    handleClose() {
      this.changePassWordPup = false;
      this.dialogVisible = false;
    },

    //跳转套餐
    handleView(id) {
      this.$router.push(`/family/restaurantManage2?id=${id}`);
    }
  }
};
</script>
      
      <style scoped>
</style>
      